﻿using UnityEngine;
using System.Collections;

public class AirBoxModel : MonoBehaviour {
    //是否变色开关
    public  bool isChanged = false;
    //子物体的材质球
    Material myMat;
    //持续时间
    public float duration = 1f;
    //变色是否返回
    bool isReset = false;
	// Use this for initialization
	void Start () {
        myMat = transform.GetChild(0).GetComponent<MeshRenderer>().material;
	}
	
	// Update is called once per frame
	void Update () {
        if (isChanged)
            ChangeColor();
        
	}
    /// <summary>
    /// 变色
    /// </summary>
    private  void ChangeColor()
    {
        if (!isReset)
        {
            myMat.color = Color.Lerp(myMat.color, Color.red, duration * Time.deltaTime);
            if(Mathf.Abs(myMat.color.g - 0f)<=0.01f)
            {
                isReset = true;
                return;
            }
        }
        else
        {
            myMat.color = Color.Lerp(myMat.color, Color.white, duration * Time.deltaTime);
            if (Mathf.Abs(myMat.color.g*255 - 255f) <= 1f)
            {
                isReset = false;
                return;
            }
        }
    }
    /// <summary>
    /// 初始化箱子颜色
    /// </summary>
    public void InitColor()
    {
        myMat.color = Color.white;
    }
}
